package cn.edu.nwpu.manager.shrio;

import cn.edu.nwpu.manager.bean.Account;
import cn.edu.nwpu.manager.service.LoginService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @author Hutengzhong
 * @version 1.0.0
 * @title ManagerAuthRealm
 * @description TODO
 * @date 2023/9/20 21:25
 */
public class ManagerAuthRealm extends AuthorizingRealm {

    @Autowired
    LoginService loginService;
    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        String username = usernamePasswordToken.getUsername();
        Account account = loginService.selectOneByUserName(username);
        String password = "";
        if(account!=null){
            password = account.getUserPassWord();
        }
        return new SimpleAuthenticationInfo(account,password,getName());
    }

    /**
     * 权限
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }


}
